We created pages for the insertion an displaying of record. In this session, we will be covering updation and deletion of data of data.
Note: Update is considerably similar to Add
products
Create Delete page:
And that's all from the series of articles. We created a complete crud operation system using a simle set of data.
Create Update page:
It works same as add function. But after ensuring that all the
entries are valid it’ll update the data using $_POST variable.
Query for update:
1 | $sql = "UPDATE products set Pro_name = ?, Pro_description =?, Pro_price =? WHERE Pro_id = ?"; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | <?php require 'main.php'; $Pro_id = null; if ( !empty($_GET['Pro_id'])) { $Pro_id = $_REQUEST['Pro_id']; } if ( null==$Pro_id ) { header("Location: index.php"); } if ( !empty($_POST)) { // keep track validation errors $Pro_nameError = null; $Pro_descriptionError = null; $Pro_priceError = null; // keep track post values $Pro_name = $_POST['Pro_name']; $Pro_description = $_POST['Pro_description']; $Pro_price = $_POST['Pro_price']; // validate input $valid = true; if (empty($Pro_name)) { $Pro_nameError = 'Please enter Name'; $valid = false; } if (empty($Pro_description)) { $Pro_descriptionError = 'Please enter Pro_description'; $valid = false; } if (empty($Pro_price)) { $Pro_priceError = 'Please enter Pro_price'; $valid = false; } // update data if ($valid) { $pdo = Products::connect(); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "UPDATE products set Pro_name = ?, Pro_description =?, Pro_price =? WHERE Pro_id = ?"; $q = $pdo->prepare($sql); $q->execute(array($Pro_name,$Pro_description,$Pro_price,$Pro_id)); Products::disconnect(); header("Location: index.php"); } } else { $pdo = Products::connect(); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "SELECT * FROM products where Pro_id = ?"; $q = $pdo->prepare($sql); $q->execute(array($Pro_id)); $data = $q->fetch(PDO::FETCH_ASSOC); $Pro_name = $data['Pro_name']; $Pro_description = $data['Pro_description']; $Pro_price = $data['Pro_price']; Products::disconnect(); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <link href="css/bootstrap.min.css" rel="stylesheet"> <link rel="stylesheet" href="css/font-awesome.min.css"> <script src="js/bootstrap.min.js"></script> <link href="css/style.css" rel="stylesheet" /> </head> <body> <div class="container"> <div class="span10 offset1"> <div class="row"> <h3>Update Products</h3> </div> <form class="form-horizontal" action="update.php?Pro_id=<?php echo $Pro_id?>" method="post"> <div class="control-group <?php echo !empty($Pro_nameError)?'error':'';?>"> <label class="control-label">Name</label> <div class="controls"> <input name="Pro_name" type="text" placeholder="Name" value="<?php echo !empty($Pro_name)?$Pro_name:'';?>"> <?php if (!empty($Pro_nameError)): ?> <span class="help-inline"><?php echo $Pro_nameError;?></span> <?php endif;?> </div> </div> <div class="control-group <?php echo !empty($Pro_descriptionError)?'error':'';?>"> <label class="control-label">Description</label> <div class="controls"> <input name="Pro_description" type="text" placeholder="Pro_description" value="<?php echo !empty($Pro_description)?$Pro_description:'';?>"> <?php if (!empty($Pro_descriptionError)): ?> <span class="help-inline"><?php echo $Pro_descriptionError;?></span> <?php endif;?> </div> </div> <div class="control-group <?php echo !empty($Pro_priceError)?'error':'';?>"> <label class="control-label">Price</label> <div class="controls"> <input name="Pro_price" type="text" placeholder="Pro_price" value="<?php echo !empty($Pro_price)?$Pro_price:'';?>"> <?php if (!empty($Pro_priceError)): ?> <span class="help-inline"><?php echo $Pro_priceError;?></span> <?php endif;?> </div> </div> <div class="form-actions"> <a type="submit" class="btn"><i class="fa fa-pencil-square-o"></i>Update</a> <a class="btn" href="index.php"><i class="fa fa-arrow-left"></i>Back</a> </div> </form> </div> </div> <!-- /container --> </body> </html> |
Create Delete page:
Firstly it will capture the $id from $_GET request.
Once a $_GET request is ended. It shows a confirmation page. If a $_POST
request is passed, it indicates that user has clicked “Yes” button. Then it
will proceed to delete the data, after deleting the record it will redirect to
"index.php" page.
Query for delete:
1 | $sql = "DELETE FROM products WHERE Pro_id = ?"; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | <?php require 'main.php'; $Pro_id = 0; if ( !empty($_GET['Pro_id'])) { $Pro_id = $_REQUEST['Pro_id']; } if ( !empty($_POST)) { // keep track post values $Pro_id = $_POST['Pro_id']; // delete data $pdo = Products::connect(); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "DELETE FROM products WHERE Pro_id = ?"; $q = $pdo->prepare($sql); $q->execute(array($Pro_id)); Products::disconnect(); header("Location: index.php"); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <link href="css/bootstrap.min.css" rel="stylesheet"> <link rel="stylesheet" href="css/font-awesome.min.css"> <script src="js/bootstrap.min.js"></script> <link href="css/style.css" rel="stylesheet" /> </head> <body> <div class="container"> <div class="span10 offset1"> <div class="row"> <h3>Delete Product</h3> </div> <form class="form-horizontal" action="deleteP.php" method="post"> <input type="hidden" name="Pro_id" value="<?php echo $Pro_id;?>"/> <p class="alert alert-error">Are you sure to delete ?</p> <div class="form-actions"> <a type="submit" class="btn "><i class="fa fa-check"></i>Yes</a> <a class="btn" href="index.php"><i class="fa fa-ban"></i>No</a> </div> </form> </div> </div> <!-- /container --> </body> </html> |
And that's all from the series of articles. We created a complete crud operation system using a simle set of data.
We created pages for the insertion an displaying of record. In this session, we will be covering updation and deletion of data of data. ...